import Page
from Page import Service
from Base.Base import Base
from selenium.webdriver.common.by import By


class ServiceTimePage(Base):
    """
    时间选择
    """

    def __init__(self, driver):
        Base.__init__(self, driver)

        # 左侧的home键
        self.time_home_btn = (By.XPATH, Service.RESOURCE_ID.format("ivHome"))

        # 左侧的返回键
        self.time_back_btn = (By.XPATH, Service.RESOURCE_ID.format('ivBack'))

        # 时间选择页面标题
        self.time_day_title_text = (By.XPATH, Page.TEXT.format("日期"))

        # 时间选择日期
        self.time_day_date_btn = (By.XPATH, Service.RESOURCE_ID.format("date"))

        # 预约时间段标题
        self.time_day_appointment_text = (By.XPATH, Page.TEXT.format("预约时间段"))

        # 时间段
        self.time_reservation_btn = (By.XPATH, Page.TEXT.format(""))

        # 确认按钮
        self.time_submit_btn = (By.XPATH, Service.RESOURCE_ID.format("time_submit"))

    def click_home(self):
        """
        点击左侧的home键

        :return:None
        """
        self.click_element(self.time_home_btn)

    def click_back(self):
        """
        点击左侧的back键

        :return:None
        """
        self.click_element(self.time_back_btn)

    def get_day_title(self):
        """
        获取日期页标题

        :return: 获取元素
        """
        return self.find_element_o(self.time_day_title_text)

    def get_day_date(self):
        """
        获取可约时间

        :return: 获取元素
        """
        return self.find_element_o(self.time_day_date_btn)

    def click_day_data(self):
        """
        点击可约时间

        :return: None
        """
        self.click_element(self.time_day_date_btn)

    def get_day_appointment(self):
        """
        获取可约时间段标题

        :return: 获取元素
        """
        return self.find_element_o(self.time_day_appointment_text)

    def click_reservation(self, index='8:00-9:00'):
        """
        点击可约时间段

        index 为默认时间段8:00-9:00
        :return: 获取元素
        """
        if index == '16:00-17:00':
            self.swipe_time_reservation(index='16:00-17:00')
            self.time_reservation_btn = (By.XPATH, Page.TEXT.format(index))
            self.click_element(self.time_reservation_btn)
        else:
            self.time_reservation_btn = (By.XPATH, Page.TEXT.format(index))
            self.click_element(self.time_reservation_btn)

    def click_submit(self):
        """
        点击确认按钮

        :return: 获取元素
        """
        self.click_element(self.time_submit_btn)

    def get_submit(self):
        """
        获取确认按钮状态

        :return: 获取元素
        """
        return self.find_element_o(self.time_submit_btn)

    def swipe_time_reservation(self, index):
        """
        滑动时间段

        :return: None
        """
        if index == '16:00-17:00':
            self.swipe_o(location=(592, 592, 360, 505), direction='up')
        else:
            self.swipe_o(location=(592, 592, 360, 505), direction='down')

    def is_display_service_time(self):
        """
        是否进入时间列表选择

        :return: bool
        """
        return self.is_display(self.time_day_title_text)
